package cn.chendd.api.nacos.controller;

import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
import io.swagger.annotations.*;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Nacos api controller
 *
 * @author chendd
 * @date 2022/9/25 21:44
 */
@RestController
@RequestMapping(value = "/nacos" , produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "NacosDiscoveryClient 测试")
@ApiSort(10)
public class NacosClientExampleController {

    @Resource
    private NacosDiscoveryClient nacosDiscoveryClient;

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "实例名列表" , notes = "获取所有实例名称列表")
    @GetMapping("/services")
    public List<String> getServices() {
        return nacosDiscoveryClient.getServices();
    }

    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "实例描述信息" , notes = "获取NacosDiscoveryClient描述信息")
    @GetMapping("/description")
    public String getDescription() {
        return nacosDiscoveryClient.description();
    }

    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "实例对象列表" , notes = "获取所有实例对象列表")
    @GetMapping("/services/{serviceId}")
    public List<ServiceInstance> getServiceId(@PathVariable @ApiParam(name = "serviceId" , value = "实例Id") String serviceId) {
        return nacosDiscoveryClient.getInstances(serviceId);
    }

}
